﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SD.Entities;
using SD.DAL;
using SD.IDAL;
using SD.IBUS;

namespace SD.BUS
{
    public class AddressBIZ : BaseBIZ, IAddressBIZ
    {
        #region IAddressBIZ Members

        /// <summary>
        /// Get existing address
        /// </summary>
        /// <returns>List of address</returns>
        public DataTable ListAddress()
        {
            DataTable dtbResult = new DataTable();
            dtbResult.Columns.Add("AddressId");
            dtbResult.Columns.Add("Address");
            dtbResult.Columns.Add("Postcode");
            dtbResult.Columns.Add("Town");
            dtbResult.Columns.Add("County");
            dtbResult.Columns.Add("Country");
            dtbResult.Columns.Add("Line2");
            dtbResult.Columns.Add("Line3");

            try
            {
                //Get list address
                List<Address> lstAddress = GetAll();
                //If rows of list >0
                if (lstAddress.Count > 0)
                {
                    //Get value of address in list address
                    foreach (Address address in lstAddress)
                    {
                        dtbResult.Rows.Add(address.AddressId, address.Line1, address.PostCode, 
                                            address.Town != null ? address.Town.Name : "N/A",
                                            address.County != null ? address.County.Name : "N/A",
                                            address.Country != null ? address.Country.Name : "N/A",
                                            address.Line2, address.Line3);
                    }
                }
            }
            catch (SqlException ex)
            {
                throw ex;
            }

            return dtbResult;
        }

        /// <summary>
        /// Get all existing address
        /// </summary>
        /// <returns>List of address</returns>
        public List<Address> GetAll()
        {
            List<Address> lstResult = new List<Address>();

            try
            {
                IAddressDAO addressDAO = new AddressDAO();
                lstResult = addressDAO.GetAll();
            }
            catch (SqlException ex)
            {
                throw ex;
            }

            return lstResult;
        }

        /// <summary>
        /// Process exception for GetByPostCode
        /// </summary>
        /// <param name="postCode"></param>
        /// <returns> NotImplementedException </returns>
        public List<Address> GetByPostCode(string postCode)
        {
            throw new NotImplementedException();
        }

        /// <summary>
        ///  Process exception for GetById
        /// </summary>
        /// <param name="addressId"></param>
        /// <returns>NotImplementedException</returns>
        public Address GetById(string addressId)
        {
            Address adress = new Address();
            IAddressDAO adressDAO = new AddressDAO();
            adress = adressDAO.GetById(addressId);
            return adress;
        }

        /// <summary>
        /// Insert address
        /// </summary>
        /// <param name="address"></param>
        /// <returns>true of false</returns>
        public bool Insert(Address address)
        {
            IAddressDAO addressDAO = new AddressDAO();
            bool result = false;

            try
            {
                result = addressDAO.Insert(address);
            }
            catch (SqlException)
            {
                throw;
            }

            return result;
        }

        /// <summary>
        /// Update address
        /// </summary>
        /// <param name="address"></param>       
        public bool Update(Address address)
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// Delete Address
        /// </summary>
        /// <param name="addressId"></param>
        public bool Delete(string addressId)
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// Get data table address with conditions
        /// </summary>
        /// <param name="postcode"></param>
        /// <param name="street"></param>
        /// <param name="town"></param>
        /// <returns>Data table address</returns>
        public DataTable ListAddressByCodition(string postcode, string street, string town)
        {
            DataTable dtbResult = new DataTable();
            dtbResult.Columns.Add("AddressId");
            dtbResult.Columns.Add("Address");
            dtbResult.Columns.Add("Postcode");
            dtbResult.Columns.Add("Town");
            dtbResult.Columns.Add("County");
            dtbResult.Columns.Add("Country");
            dtbResult.Columns.Add("Line2");
            dtbResult.Columns.Add("Line3");

            try
            {
                List<Address> lstAddress = GetByCodition(postcode, street, town);
                //If number row of list address >0
                if (lstAddress.Count > 0)
                {
                    //Get address from list address
                    foreach (Address address in lstAddress)
                    {
                        dtbResult.Rows.Add(address.AddressId, address.Line1, address.PostCode,
                                            address.Town.Name, address.County.Name, address.Country.Name,
                                            address.Line2, address.Line3);
                    }
                }
            }
            catch (SqlException ex)
            {
                throw ex;
            }

            return dtbResult;
        }

        public List<Address> GetByCodition(string postcode, string street, string town)
        {
            List<Address> lstResult = new List<Address>();

            try
            {
                IAddressDAO addressDAO = new AddressDAO();
                lstResult = addressDAO.GetByCodition(postcode, street, town);
            }
            catch (SqlException ex)
            {
                throw ex;
            }

            return lstResult;
        }

        #endregion
    }
}
